package co.baiku.boot.config;

import co.baiku.boot.cache.type.CacheEngine;
import co.baiku.boot.common.tools.StringTools;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Configuration
@ConditionalOnProperty("ajavaer.weibo.appid")
@ConfigurationProperties(prefix = "ajavaer.weibo")
@ComponentScan(basePackages = {"co.baiku.boot.config", "co.baiku.boot.weibo"})
public class WeiboConfig {

    private String appid;
    private String secret;
    private CacheEngine cacheEngine = CacheEngine.caffeine;
    private String cachePrefix = "weibo_";
    private String codeUrl = "https://api.weibo.com/oauth2/authorize?client_id={0}&response_type=code&redirect_uri={1}";
    private String accessTokenUrl = "https://api.weibo.com/oauth2/access_token?client_id={0}&client_secret={1}&grant_type=authorization_code&redirect_uri={2}&code={3}";
    private String userInfoUrl = "https://api.weibo.com/2/users/show.json?access_token={0}&uid={1}";


    public String getAccessTokenUrl(String redirect_uri, String code) {
        return StringTools.format(this.accessTokenUrl, appid, secret, redirect_uri, code);
    }

    public String getUserInfoUrl(String access_token, String uid) {
        return StringTools.format(this.userInfoUrl, access_token, uid);
    }

    public String getCodeUrl(String redirect_uri) {
        try {
            return StringTools.format(this.codeUrl, appid, secret, URLEncoder.encode(redirect_uri, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }
        return "";
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public void setAccessTokenUrl(String accessTokenUrl) {
        this.accessTokenUrl = accessTokenUrl;
    }

    public void setUserInfoUrl(String userInfoUrl) {
        this.userInfoUrl = userInfoUrl;
    }

    public void setCodeUrl(String codeUrl) {
        this.codeUrl = codeUrl;
    }

    public CacheEngine getCacheEngine() {
        return cacheEngine;
    }

    public void setCacheEngine(CacheEngine cacheEngine) {
        this.cacheEngine = cacheEngine;
    }

    public String getCachePrefix() {
        return cachePrefix;
    }

    public void setCachePrefix(String cachePrefix) {
        this.cachePrefix = cachePrefix;
    }

}
